package com.echo.yixiaowu.feature.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.echo.yixiaowu.ui.theme.Pink

@Composable
fun VideoProgressBar(
    modifier: Modifier = Modifier,
    isShowColor: Boolean = true,
    isShowThumb: Boolean,
    isShowDrag: Boolean = false,
    progress: Float = 0.5f,
    bufferProgress: Float = 0.5f,
    thumbRadius: Dp = 16.dp,
    thumbPosition: Float = 0f,
    trackHeight: Dp = 2.dp,
    onProgressChange: ((Float) -> Unit)? = null,
    onThumbChange: (Float) -> Unit = {},
    onBarWidthChange: (Float) -> Unit = {}
) {
    var thumbX by remember { mutableFloatStateOf(thumbPosition) }
    var barWidth by remember { mutableFloatStateOf(1f) }
    var isDrag by remember { mutableStateOf(isShowDrag) }
    var newProgress by remember { mutableFloatStateOf(0f) }
    val moveScale by animateFloatAsState(
        targetValue = when {
            isDrag || isShowDrag -> 1.3f
            isShowThumb -> 1f
            else -> 0f
        }, animationSpec = tween(durationMillis = 500)
    )

    Layout(
        modifier = modifier
            .draggable(state = rememberDraggableState {
                if (isDrag) {
                    println("thumbX: $thumbX + $it")
                    thumbX = (thumbX + it).coerceIn(0f, barWidth)
                    newProgress = thumbX / barWidth
                    println("thumbX: $newProgress + $thumbX / $barWidth")
                }
            }, orientation = Orientation.Horizontal,
                onDragStarted = {
                    isDrag = true

                },
                onDragStopped = {
                    isDrag = false
                    onProgressChange?.invoke(newProgress)
                }
            ),
        content = {
            // 轨道
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(trackHeight)
                    .then(if (isShowColor) Modifier.background(Color.White.copy(0.2f)) else Modifier)
            )
            // 缓冲条
            Box(
                modifier = Modifier
                    .fillMaxWidth(bufferProgress) // 按比例填充
                    .height(trackHeight)
                    .then(if (isShowColor) Modifier.background(Color.White.copy(0.4f)) else Modifier)
            )
            //进度条
            Box(
                modifier = Modifier
                    .fillMaxWidth(progress)
                    .height(trackHeight)
                    .background(Pink)
            )
            //滑块
            Box(modifier = Modifier
                .size(thumbRadius)
                .aspectRatio(1f)
                .graphicsLayer {
                    scaleX = moveScale
                    scaleY = moveScale
                }
                .clip(CircleShape)
                .background(Color.Red, CircleShape)
            )
        }, measurePolicy = { measurable, constraint ->
            val track = measurable[0].measure(constraint) // 轨道
            val bufferProgressTrack = measurable[1].measure(constraint) // 缓冲条
            val progressTrack = measurable[2].measure(constraint) // 进度条
            val thumb = measurable[3].measure(constraint) // 滑块

            barWidth = constraint.maxWidth.toFloat()
            onBarWidthChange(barWidth)

            thumbX = when {
                isDrag -> thumbX
                else -> progress * barWidth
            }
            if (!isShowDrag) {
                onThumbChange(thumbX)
            }
            layout(constraint.maxWidth, thumb.height) {
                val trackCenterY = thumb.height / 2 - track.height / 2
                track.place(0, trackCenterY)
                bufferProgressTrack.place(0, trackCenterY)
                progressTrack.place(0, trackCenterY)
                thumb.place(thumbX.toInt() - thumb.width / 2, 0)
            }
        })
}